<?php
class AppController extends Controller {
    var $helpers = array('Html', 'Form','Javascript');

    function checkSession(){
        if(!$this->Session->check('Usuario')){
            $this->Session->write('url_redirect', $this->params['url']['url']);
            $this->redirect('/usuarios/login');
            exit;
        }
    }

    function beforeFilter(){
        $this->loadModel('Edicao');
        $ultima_edicao = $this->Edicao->find('first', array('order'=>array('Edicao.id DESC')));
        $this->set('ultima_edicao', $ultima_edicao['Edicao']['id']);

        $this->loadModel('Mondano');
        $menuMondanos = $this->Mondano->find('all',
                array('fields'=>array('slug', 'nick'),
                    'conditions'=>array('Mondano.colaborador'=>'0')));
        $menuColaboradores = $this->Mondano->find('all',
                array('fields'=>array('slug', 'nick'),
                    'conditions'=>array('Mondano.colaborador'=>'1')));
        $this->loadModel('Secao');
        $menuSecoes = $this->Secao->find('all', array('conditions'=>array('Secao.slug <>'=>'mondo-redondo')));
        $this->set(compact('menuMondanos','menuColaboradores','menuSecoes'));
        $paginasRestritas = array('novo','edit');

        if(isset($this->params['admin']) || in_array($this->params['action'], $paginasRestritas)){
            $this->checkSession();
        }
        if($this->Session->check('Usuario')){
            $this->set('logado', 1);
            $this->set('usuario', $this->Session->read('Usuario.email'));
            $this->set('usuario_nick', $this->Session->read('Usuario.nick'));
            $this->set('usuario_id', $this->Session->read('Usuario.id'));
        }else
            $this->set('logado', 0);

    }
}
?>